﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace Smartec.Accounts.Camel.AMS.Model.Entities
{
    [MetadataType(typeof(DBAuditDetail_Metadata))]
    public partial class DBAuditDetail
    {
        #region Audit
        private string dbAuditName;
        [DisplayName("Audit")]
        public string Audit
        {
            get
            {
                if (dbAuditName == null)
                {
                    dbAuditName = DBAudit == null ? "" : string.Format("[<b>{0}</b>] - [<span style='color:red;'><b>{1}</b></span>] - [<span style='color:blue;'><b>{2}</b></span>]- [<span style='color:green;'><b>{3}</b></span>]- [{4}]", DBAudit.RevisionStamp.ToString("dd-MM-yyyy HH:mm:ss"), DBAudit.TableName, MasterName, DBAudit.UserName, DBAudit.AuditId);
                }

                return dbAuditName;
            }
            set
            {
                dbAuditName = value;
            }
        }
        #endregion //Audit

        #region Changes
        private string changes;
        [DisplayName("Changes")]
        public string Changes
        {
            get
            {
                if (changes == null)
                {
                    changes = "";
                    if (!string.IsNullOrEmpty(ChangedColumns))
                    {
                        //try
                        //{
                       
                        changes += "<table class='gridtable'><tr><th>Property</th><th>Old value</th><th>New value</th></tr>";
                        foreach (string preoperty in ChangedColumns.Split('!'))
                        {
                            string[] propertyItems = preoperty.Split('$');
                            string masterTableName = propertyItems[0];
                            string propertyName = propertyItems[1];
                            string oldValue = propertyItems[2];
                            string newValue = propertyItems[3];

                            if (!string.IsNullOrEmpty(masterTableName))
                            {
                                int oldId=-1;
                                if (Int32.TryParse(oldValue, out oldId))
                                {
                                   oldValue = AMSObjectContextHelper.GetItemNameByTableNameAndID(masterTableName, oldId);
                                }

                                int newId = -1;
                                if (Int32.TryParse(newValue, out newId))
                                {
                                    newValue = AMSObjectContextHelper.GetItemNameByTableNameAndID(masterTableName, newId);
                                }
                            }
                            changes += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", propertyName.EndsWith("_ID") ? propertyName.Substring(0, propertyName.LastIndexOf("_ID")) : propertyName, oldValue, newValue);
                        }
                        changes += "</table>";
                        //}
                        //catch
                        //{

                        //}
                    }
                }

                return changes;
            }
            set
            {
                changes = value;
            }
        }
        #endregion //Changes

        #region RevisionStamp
        private DateTime? revisionStamp;
        [DisplayName("Date")]
        public DateTime? RevisionStamp
        {
            get
            {
                if (revisionStamp == null)
                {
                    revisionStamp = DBAudit == null ? null : (DateTime?)DBAudit.RevisionStamp;
                }

                return revisionStamp;
            }
            set
            {
                revisionStamp = value;
            }
        }
        #endregion //RevisionStamp

        #region UserName
        private string userName;
        [DisplayName("User Name")]
        public string UserName 
        {
            get
            {
                if (userName == null)
                {
                    userName = DBAudit == null ? "" : DBAudit.UserName;
                }

                return userName;
            }
            set
            {
                userName = value;
            }
        }
        #endregion //UserName

        #region MasterEntity
        private string masterTable;
        [DisplayName("Master Type")]
        public string MasterEntity
        {
            get
            {
                if (masterTable == null)
                {
                    if (DBAudit == null)
                        masterTable = "";
                    else
                    {
                        masterTable = DBAudit.TableName;
                        //if (masterTable.EndsWith("ies"))
                        //{
                        //    masterTable = masterTable.Substring(0, masterTable.LastIndexOf("ies")+1);
                        //}
                        //else if (masterTable.EndsWith("es"))
                        //{
                        //    masterTable = masterTable.Substring(0, masterTable.LastIndexOf("es")+1);
                        //}
                        //else if (masterTable.EndsWith("s"))
                        //{
                        //    masterTable = masterTable.TrimEnd('s');
                        //}
                    }
                }

                return masterTable;
            }
            set
            {
                masterTable = value;
            }
        }
        #endregion //MasterEntity

        #region MasterName
        private string masterName;
        [DisplayName("Master Item Name")]
        public string MasterName
        {
            get
            {
                if (masterName == null)
                {
                    masterName = DBAudit == null ? "" : AMSObjectContextHelper.GetItemNameByTableNameAndID(DBAudit.TableName, DBAudit.MasterId.Value);
                }

                return masterName;
            }
            set
            {
                masterName = value;
            }
        }
        #endregion //MasterName

        #region EntityName
        private string entityName;
        [DisplayName("Item Name")]
        public string EntityName
        {
            get
            {
                if (entityName == null)
                {
                    entityName = AMSObjectContextHelper.GetItemNameByTableNameAndID(TableName, EntityId.Value);
                }

                return entityName;
            }
            set
            {
                entityName = value;
            }
        }
        #endregion //EntityName

        #region AuditId
        private string auditId;
        [DisplayName("audit Id")]
        public string AuditId
        {
            get
            {
                if (auditId == null)
                {
                    auditId = DBAudit == null ? "" : DBAudit.AuditId.ToString();
                }

                return auditId;
            }
            set
            {
                auditId = value;
            }
        }
        #endregion //AuditId

        //#region TableDisplayName
        //private string tableDisplayName;
        //[DisplayName("Item Type")]
        //public string TableDisplayName
        //{
        //    get
        //    {
        //        if (tableDisplayName == null)
        //        {
        //            tableDisplayName = TableName;
        //            if (tableDisplayName.EndsWith("ies"))
        //            {
        //                tableDisplayName = tableDisplayName.Substring(0, tableDisplayName.LastIndexOf("ies"));
        //            }
        //            else if (tableDisplayName.EndsWith("es"))
        //            {
        //                tableDisplayName = tableDisplayName.Substring(0, tableDisplayName.LastIndexOf("es"));
        //            }
        //            else if (tableDisplayName.EndsWith("s"))
        //            {
        //                tableDisplayName = tableDisplayName.TrimEnd('s');
        //            }
        //        }

        //        return tableDisplayName;
        //    }
        //    set
        //    {
        //        tableDisplayName = value;
        //    }
        //}
        //#endregion //TableDisplayName
    }


    public class DBAuditDetail_Metadata
    {

        [DisplayName("Item Type")]
        public string TableName { get; set; }

        [DisplayName("Action")]
        public string Action { get; set; }

        [DisplayName("Changed Fields")]
        public string ChangedColumns { get; set; }

        [DisplayName("Name")]
        public string EntityId { get; set; }
    }
}
